<template>
  <el-breadcrumb class="breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item
        v-for="(breadcrumb, index) of breadcrumbData"
        :key="breadcrumb.path"
      >
        <!-- 不可点击项：最后一项 -->
        <span class="no-redirect" v-if="index === breadcrumbData.length - 1">{{
          generateTitle(breadcrumb.meta.title)
        }}</span>
        <!-- 可点击 -->
        <span class="redirect" v-else @click="() => onLinkClick(breadcrumb)">{{
          generateTitle(breadcrumb.meta.title)
        }}</span>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script setup>
import { generateTitle } from '@/utils/i18n';
import { ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';

// 生成数组数据
const breadcrumbData = ref([]);
const getBreadcrumbData = () => {
  // 通过route.matched获取到当前路由的路由表
  breadcrumbData.value = route.matched.filter(
    (item) => item.meta && item.meta.title
  );
};

// 监听路由变化
const route = useRoute();
watch(
  route,
  () => {
    getBreadcrumbData();
  },
  {
    immediate: true
  }
);

// 点击跳转事件
const router = useRouter();
const onLinkClick = (item) => {
  router.push(item.redirect || item.path);
};
</script>

<style lang='scss' scoped>
.breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }

  .redirect {
    color: #666;
    font-weight: 600;
    cursor: pointer;
  }

  .redirect:hover {
    // 将来需要进行主题替换，所以这里不去写死样式
    color: v-bind(linkHoverColor);
  }
}
</style>
